<?php

class DeleteStepAction extends CAction
{
    public function run()
    {
        if(!Yii::app()->request->isPostRequest || !isset($_POST['step']) || !isset($_GET['id']) )
            throw new CHttpException(400,'the request is invalid');

        $recipe=Recipe::loadObjectById($_GET['id'],array('userId','steps'));
        if($recipe===null)
            throw new CHttpException(404,'The requested recipe does not exist.');
        if($recipe->userId!=Yii::app()->user->id)
            throw new CHttpException(403,'cannot edit the recipe not belong to you');

        $step=Step::loadObjectById($_POST['step'],array('image'));
        if($step===null)
            throw new CHttpException(404,'The requested step does not exist.');
        if($step->recipeId!=$_GET['id'])
            throw new CHttpException(403,'cannot edit the step not belong to the recipe');
        if($step->userId!=Yii::app()->user->id)
            throw new CHttpException(403,'cannot edit the step not belong to you');
        
        $succ=Recipe::deleteStep($recipe, $_POST['step']);
        if($succ) {
            UserImage::delete('step', $step->image);
        }
        
        $this->controller->renderPartial('/common/json',array(
            'data'=>array(
                'succ'=>$succ,
                'error'=>$recipe->getError('steps'),
        )));
    }
}

?>